package com.vipboot.framework.web.autoconfig;

import com.vipboot.framework.core.constant.WebFilterOrderConst;
import com.vipboot.framework.web.requestbody.RequestBodyCacheFilter;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

@Slf4j
@AutoConfiguration
@ConditionalOnWebApplication
public class RequestBodyCacheAutoConfig {

    @PostConstruct
    public void postConstruct() {
        log.info("初始化 RepeatedlyAutoConfig");
    }

    /**
     * 创建 RequestBodyCacheFilter Bean，可重复读取请求内容
     */
    @Bean
    public FilterRegistrationBean<RequestBodyCacheFilter> cacheRequestBodyFilter() {
        FilterRegistrationBean<RequestBodyCacheFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new RequestBodyCacheFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setName("repeatedlyFilter");
        registrationBean.setOrder(WebFilterOrderConst.RequestBodyCacheFilter);
        return registrationBean;
    }
}
